package com.sgj.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.sgj.common.utils.R;
import com.sgj.gulimall.ware.feign.MemberFeignService;
import com.sgj.common.vo.FareVo;
import com.sgj.common.vo.MemberAddressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgj.common.utils.PageUtils;
import com.sgj.common.utils.Query;

import com.sgj.gulimall.ware.dao.WareInfoDao;
import com.sgj.gulimall.ware.entity.WareInfoEntity;
import com.sgj.gulimall.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.eq("id", key)
                    .or().like("name", key)
                    .or().like("address", key)
                    .or().like("areacode", key);
        }

        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R r = memberFeignService.info(addrId);
        MemberAddressVo addressVo = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {});
        BigDecimal fare = BigDecimal.ZERO;
        if (Objects.nonNull(addressVo)) {
            switch (addressVo.getProvince()) {
                case "湖北省" :
                case "湖南省" :
                case "福建省" : fare = new BigDecimal(12); break;
                case "黑龙江省" :
                case "广东省" :
                case "吉林省" :
                case "辽宁省" :
                case "河北省" :
                case "山东省" :
                case "四川省" :
                case "云南省" :
                case "贵州省" :
                case "青海省" :
                case "甘肃省" : fare = new BigDecimal(16); break;
                case "海南省" :
                case "台湾省" : fare = new BigDecimal(24); break;
                default : fare = new BigDecimal(8); break;
            }
            fareVo.setAddress(addressVo);
            fareVo.setFare(fare);
        }
        return fareVo;
    }
}